package com.example.gw_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gw_back_end.domain.PageResult;
import com.example.gw_back_end.domain.entities.Friendship;
import com.example.gw_back_end.domain.entities.User;
import com.example.gw_back_end.mapper.FriendshipMapper;
import com.example.gw_back_end.mapper.UserMapper;
import com.example.gw_back_end.service.FriendshipService;
import com.example.gw_back_end.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class FriendshipServiceImpl extends ServiceImpl<FriendshipMapper, Friendship> implements FriendshipService {

    private final FriendshipMapper friendshipMapper;
    private final UserMapper userMapper;
    public FriendshipServiceImpl(FriendshipMapper friendshipMapper, UserMapper userMapper) {
        this.friendshipMapper = friendshipMapper;
        this.userMapper = userMapper;
    }

    @Override
    public PageResult<Friendship> getFriendshipList() {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");

        LambdaQueryWrapper<Friendship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Friendship::getUserId, userId);
        queryWrapper.eq(Friendship::getStatus,0);

        List<Friendship> list = friendshipMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
        List<Integer> friendIds = list.stream()
                .map(Friendship::getFriendId)  // 提取所有好友ID
                .collect(Collectors.toList());

        List<User> users = userMapper.selectBatchIds(friendIds);

        Map<Integer, User> userMap = users.stream()
                .collect(Collectors.toMap(User::getId, user -> user));
        for (Friendship friendship : list) {
            User friend = userMap.get(friendship.getFriendId());  // 通过friendId获取对应的User对象
            if (friend != null) {
                friendship.setUsername(friend.getUsername());  // 设置好友的用户名
                friendship.setAvatar(friend.getAvatar());  // 设置好友的头像
            }
        }

        Page<Friendship> friendshipPage = new Page<>();
        friendshipPage.setRecords(list);
        friendshipPage.setTotal(list.size());
        friendshipPage.setSize(list.size());
        friendshipPage.setCurrent(1);
        return PageResult.setPageResult(friendshipPage);
        } else {
            return null;
        }
    }

    @Override
    public boolean addFriendship(Integer friendUserId) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        LambdaQueryWrapper<Friendship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Friendship::getUserId, userId);
        queryWrapper.eq(Friendship::getFriendId,friendUserId);
        friendshipMapper.delete(queryWrapper);
        Friendship friendship = new Friendship();
        friendship.setUserId(userId);
        friendship.setFriendId(friendUserId);
        friendship.setStatus(1);
        friendship.setCreateDate(LocalDateTime.now());
        return friendshipMapper.insert(friendship) > 0;
    }

    @Override
    public boolean updateFriendship(Friendship friendship) {
        return friendshipMapper.updateById(friendship) > 0;
    }

}
